package com.smart.base.taskTest;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
 * Callable 接口类似于Runnable 但是具有返回值
 * 
 * 启动线程FutureTask去做业务，
 * 然后不用等待，下面事情，
 * 然后调用future.get 会堵塞知道线程future昨晚并返回值。
 * 
 * @author gebao
 *
 */
public class FutureTest {
	public static void main(String[] args) {
		Callable call = new Callable(){
			@Override
			public Object call() throws Exception {
				Thread.sleep(8000);
				System.out.println("Callable call in");
				return "Callable call in";
			}
			
		};
		FutureTask future = new FutureTask(call);
		new Thread(future).start();
		try {
			for (int i = 0; i < 5; i++) {
				System.out.println("do something "+i);
				Thread.sleep(1000);
			}
			System.out.println("return:"+future.get());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
